package com.wyp168.leetcode;

/**
 * @ClassName NearestValidPointSolution
 * @Title TODO Leetcode 1779
 * @Description TODO 找到最近的有相同 X 或 Y 坐标的点; Level: Easy
 * @Author wyp168
 * @Date 2022/12/1 08:09
 */
public class NearestValidPointSolution {
    public static void main(String[] args) {
        int x = 3, y = 4;
        int[][] points = {{1,2}, {3,1}, {2,4}, {2,3}, {4,4}};

        NearestValidPointSolution solution = new NearestValidPointSolution();
        System.out.println(solution.nearestValidPoint(x, y, points));
    }

    public int nearestValidPoint(int x, int y, int[][] points) {
        double[] minIndex = {-1, 0, 10004};

        for (int i = 0; i < points.length; i ++) {
            int[] point = points[i];
            if (point[0] != x && point[1] != y) {
                continue;
            }

            double dist = Math.sqrt(Math.pow(point[0] - x, 2) + Math.pow(point[1] - y, 2));

            if (dist < minIndex[2]) {
                minIndex[0] = i;
                minIndex[1] = point[0] +point[1];
                minIndex[2] = dist;
            }

            if (dist == minIndex[1]) {
                int IndexSite = point[0] + point[1];
                if (IndexSite < minIndex[1]) {
                    minIndex[0] = i;
                    minIndex[1] = IndexSite;
                }
            }
        }
        return (int) minIndex[0];
    }
}
